package tcinert;


import com.siemens.icm.io.CommConnectionControlLines;
import com.siemens.icm.misc.Watchdog;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
/**
 * WatchDog
 * - hlidani programu, reseni pri problemech s programem
 * 
 * @Author: Tomas Cinert
 */

public class WatchDog implements Runnable{
    private boolean loop=true;
    private Thread vlakno=new Thread(this);
    Stav reset;
    Zaznam zaznam;
    int i=0;
     
    /** Creates a new instance of WatchDog */
   
    public WatchDog(Stav reset, Zaznam zaznam)
    {
        this.reset=reset;
        this.zaznam=zaznam;
         
    }
    public void start() {
        Watchdog.start( 60 );
        loop = true;
        zaznam.zapis("WatchDog startuje",typZapisu.Informativni);
        vlakno.start();
    }
    public void stop() {
        loop = false;
        try {
            vlakno.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
   
    public void run() {
        while (loop) {
            
        
            i++;        // Logovat kdy to watchdog vyrazil
            
               
                if ( reset.getStav())
                {
                    zaznam.zapis("Watchdog ukoncuje aplikaci\nByl to"+i+".prubeh",typZapisu.Chybovy);
                    Watchdog.kick();
                }
                
                
            try {
                Thread.sleep(100000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
           
        }
    }

   

    
}